package com.aaa.project.service.impl;

import com.aaa.entity.EEmpInfo;
import com.aaa.project.mapper.EEmpInfoMapper;
import com.aaa.project.service.IEEmpInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.security.Principal;

/**
 * <p>
 * 管理员信息表 服务实现类
 * </p>
 *
 * @author yuyongli
 * @since 2022-12-29
 */
@Service
public class EEmpInfoServiceImpl extends ServiceImpl<EEmpInfoMapper, EEmpInfo> implements IEEmpInfoService {

    @Autowired
    private EEmpInfoMapper eEmpInfoMapper;

    @Override
    public boolean updatePassword(String oldPassword, String newPassword, Principal principal) {
        System.out.println(principal);
        String telephone = principal.getName();
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("telephone",telephone);
        EEmpInfo eEmpInfo = eEmpInfoMapper.selectOne(queryWrapper);
        boolean matches = encoder.matches(oldPassword,eEmpInfo.getPassword());
        if (matches){
            String encode = encoder.encode(newPassword);
            return eEmpInfoMapper.updatePassword(encode,telephone);
        }
       return false;
    }
}
